package com.aipartner.config;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * AI模型配置类
 * 
 * @author AI Partner Team
 * @since 2024-01-20
 */
@Data
@Component
@ConfigurationProperties(prefix = "ai-partner.ai-models")
public class AiModelConfig {
    
    /**
     * 默认模型ID
     */
    private String defaultModel;
    
    /**
     * 可用模型列表
     */
    private List<AiModel> models;
    
    /**
     * 功能到模型的映射配置
     */
    @JsonProperty("function-model-mapping")
    private Map<String, List<String>> functionModelMapping;
    
    @Data
    public static class AiModel {
        private String id;
        private String name;
        private String description;
        private String apiUrl;
        private String apiKey;
        private String modelName;  // 添加模型名称字段，用于API调用时指定具体模型
        private boolean enabled;
        private List<String> specialties;
    }
    
    /**
     * 根据功能ID获取最适合的模型ID
     * 
     * @param functionId 功能ID
     * @return 模型ID
     */
    public String getModelForFunction(String functionId) {
        // 添加调试日志
        System.out.println("DEBUG: getModelForFunction called with functionId: " + functionId);
        System.out.println("DEBUG: functionModelMapping is null: " + (functionModelMapping == null));
        
        if (functionModelMapping == null) {
            System.out.println("DEBUG: functionModelMapping is null, returning defaultModel: " + defaultModel);
            return defaultModel;
        }
        
        System.out.println("DEBUG: functionModelMapping keys: " + functionModelMapping.keySet());
        
        // 查找功能所属的分类
        for (Map.Entry<String, List<String>> entry : functionModelMapping.entrySet()) {
            System.out.println("DEBUG: Checking category: " + entry.getKey() + " with functions: " + entry.getValue());
            if (entry.getValue().contains(functionId)) {
                String category = entry.getKey();
                String selectedModel = getModelByCategory(category);
                System.out.println("DEBUG: Found functionId in category: " + category + ", selected model: " + selectedModel);
                return selectedModel;
            }
        }
        
        // 如果没有找到特定映射，返回默认模型
        System.out.println("DEBUG: No mapping found for functionId: " + functionId + ", returning defaultModel: " + defaultModel);
        return defaultModel;
    }
    
    /**
     * 根据分类获取对应的模型ID
     * 
     * @param category 功能分类
     * @return 模型ID
     */
    private String getModelByCategory(String category) {
        switch (category) {
            case "tech":
            case "business":
            case "paper_assistant":
            case "business_marketing":
            case "survey_assistant":
                return "deepseek-chat";  // 技术/商务/论文辅助/营销/问卷使用DeepSeek Chat
            case "chinese_content":
            case "lifestyle":
            case "text_processing":
            case "work_efficiency":
            case "personal_assistant":
                return "deepseek-chat";     // 中文内容/生活/工作提效/个人助理改为使用DeepSeek
            case "creative":
            case "social_media":
            case "life_entertainment":
            case "video_creation":
            case "mj_painting":
                return "doubao-pro";   // 创意/社媒/娱乐/视频/绘画使用豆包
            default:
                return "deepseek-chat";  // 默认使用DeepSeek
        }
    }
    
    /**
     * 获取模型配置信息
     * 
     * @param modelId 模型ID
     * @return 模型配置
     */
    public AiModel getModel(String modelId) {
        if (models == null) {
            return null;
        }
        
        return models.stream()
                .filter(model -> model.getId().equals(modelId))
                .findFirst()
                .orElse(null);
    }
    
    /**
     * 检查模型是否可用
     * 
     * @param modelId 模型ID
     * @return 是否可用
     */
    public boolean isModelAvailable(String modelId) {
        AiModel model = getModel(modelId);
        return model != null && model.isEnabled() && 
               model.getApiKey() != null && !model.getApiKey().startsWith("your-");
    }
}
